﻿Imports System.Collections
Imports System.Net.Sockets
Imports System.Net

Public Class Handler

    Public workSocket As Socket = Nothing
    Public userID As Integer = -1
    Public Const BufferSize As Integer = 1024
    Public Const HeaderLen As Integer = 13
    Private buf(BufferSize) As Byte
    Private bytesRead As Integer = 0

    Public Sub New(ByVal socket As Socket)
        workSocket = socket

        Dim buffer(HeaderLen) As Byte
        buffer(0) = CByte(&H5E)
        buffer(1) = &H8
        buffer(4) = &H0
        buffer(9) = &H0
        workSocket.Send(buffer, HeaderLen, SocketFlags.None)
    End Sub

    Public Function handle() As Boolean
        Try
            bytesRead = workSocket.Receive(buf, 0, BufferSize, SocketFlags.None)
            If (bytesRead = 0) Then
                Return False
            End If
            Dim dataLength As Integer = buf(5)
            Dim ph As New PacketHandler(Me)
            ph.handlePacket(buf, HeaderLen, dataLength)
            Return True
        Catch ex As SocketException
            Return False
        Catch ex As Exception
            log.log(LogType.MSG_DEBUG, "Exception at " & ex.TargetSite.Name & ": ")
            Console.WriteLine(ex)
            Return False
        End Try
    End Function

End Class
